package com.ysynzehao;

import com.ysynzehao.bean.Admin;
import com.ysynzehao.bean.User;
import com.ysynzehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * niuzehao
 * 2018/12/4 16:29.
 */
@Configuration
public class MyAutoConfiguration {

    @Autowired
    private Environment env;

    @Bean
    @ConditionalOnMissingBean
    public User createUser() {
        User user = new User();
        user.setUserName("admin");
        user.setUserId("111");
        return user;
    }

    @Bean
    @ConditionalOnBean(User.class)
    public UserService createUserService(User user, Admin admin) {
        System.out.println(admin.toString());
        String adminName = env.getProperty("ysynzehao.admin.name", String.class);
        System.out.println(adminName);
        UserService userService = new UserService();
        userService.setUser(user);
        return userService;
    }

}
